<?php
/**
 *
 * Project:     Neuf TV MediaCenter - MP9CTL
 * File:        browse.inc.php
 *
 * Page de navigation dans dossier
 *
 * @author Julien J. pour Neuf Cegetel
 *
 **/

/* Definition des partages */
require(getConfFile());

/* Parametres */
$location = '';
$filter = FALSE;
if (isset($_GET['location'])) $location = stripslashes($_GET['location']);
if (isset($_GET['filter'])) $filter = intval($_GET['filter']);

/* Recuperation contenu dossier */
$error = FALSE;
$parentPath = FALSE;
$folders = array();
$medias = array();

$aliasDir = $location;
if (substr($aliasDir, -1, 1) != "/") $aliasDir .= "/";
$realDir = getRealPath($aliasDir);

if (is_dir($realDir)) {
    $dir = dir($realDir);
    $parentPath = urlencode(dirname($aliasDir));
	    	    
    /* Si support streamer, detection CD Audio et DVD */
    if (STREAMER_SUPPORT && isDVD($dir->path)) {
	$media = array(
	    "type" => MEDIA_DVD,
	    "name" => "DVD",
	    "urlStream" => getUrlStreamDvd($dir->path)
	);
	$medias[] = $media;
    }
    else if (STREAMER_SUPPORT && isCDAudio($dir->path)) {
	$media = array(
	    "type" => MEDIA_AUDIOCD,
	    "name" => "CD Audio",
	    "urlStream" => getUrlStreamCDAudio($dir->path)
	);
	$medias[] = $media;
    }
    /* Sinon parcours repertoire */
    else while (($file = readdir($dir->handle)) !== FALSE) {
	if (isUsableFileOrDir($dir->path.$file)) {
	    $fullFile = $dir->path.$file;
	    $fullFileAlias = $aliasDir.$file;
	    /* Traitement des dossiers */
	    if (is_dir($fullFile)) {
		$folders[] = array(
		    "name" => getDisplayNameFromFilename($fullFile),
		    "path" => urlencode($fullFileAlias)
		);
	    }
	    /* Traitement des fichiers */
	    else if (is_file($fullFile)) {
		$mediaType = getMediaTypeFromFilename($fullFile);
		
		/* Filtrage par type de media */
		if ($filter && (($mediaType & $filter) == $mediaType)) {
		    /* Media supporte nativement */
		    if (nativeSupport($fullFile)) {
			$media = array(
			    "type" => $mediaType,
			    "name" => getDisplayNameFromFilename($fullFile),
			    "urlHttp" => getUrlHttpFromFilename($fullFileAlias)
			);
			if ($mediaType == MEDIA_PLAYLIST) $media["parentPath"] = urlencode($aliasDir);
			if ($srt = srtFileExists($fullFileAlias)) $media["srt"] = getUrlHttpFromFilename($srt);
			if (externalStreamerSupport($fullFile)) $media["urlStream"] = getUrlStreamFromFilename($fullFile);
			$medias[] = $media;
		    }
		    /* Lecture via streamer externe si supporte */
		    else if (externalStreamerSupport($fullFile)) {
			$media = array(
			    "type" => $mediaType,
			    "name" => getDisplayNameFromFilename($fullFile),
			    "urlStream" => getUrlStreamFromFilename($fullFile)
			);
			if ($srt = srtFileExists($fullFileAlias)) $media["srt"] = getUrlHttpFromFilename($srt);
			$medias[] = $media;
		    }
		}
	    }
	}
    }
    /* Tri si indique dans le filtre */
    if (($filter & SORT_BY_NAME) == SORT_BY_NAME) {
	sortList($folders, 'name', (($filter & SORT_REVERSE) == SORT_REVERSE));
	sortList($medias, 'name', (($filter & SORT_REVERSE) == SORT_REVERSE));
    } else if (($filter & SORT_BY_DATE) == SORT_BY_DATE) {
	sortList($folders, 'date', (($filter & SORT_REVERSE) == SORT_REVERSE));
	sortList($medias, 'date', (($filter & SORT_REVERSE) == SORT_REVERSE));
    }
} else {
    $error = "NOT_DIRECTORY";
}

echo '<?xml version="1.0" encoding="utf-8" ?>'."\n";
?>
<browse<?= ($error ? " error=\"$error\"":"") ?>>
    <?= ($parentPath ? "<parentPath>$parentPath</parentPath>" : "") ?> 
    <folders>
<?php for($i=0; $i<count($folders); $i++) { ?>
	<folder>
	    <name><?= $folders[$i]["name"] ?></name>
	    <path><?= $folders[$i]["path"] ?></path>
	</folder>	
<?php } ?>
    </folders>
    <medias>
<?php for($i=0; $i<count($medias); $i++) { ?>
	<media type="<?= $medias[$i]["type"] ?>" >
	    <name><?= $medias[$i]["name"] ?></name>
<?php if (isset($medias[$i]["urlHttp"])) { ?>
	    <url type="http"><?= $medias[$i]["urlHttp"] ?></url>
<?php }
      if (isset($medias[$i]["urlStream"])) { ?>
	    <url type="stream"><?= $medias[$i]["urlStream"] ?></url>
<?php }
      if (isset($medias[$i]["srt"])) { ?>
	    <srt><?= $medias[$i]["srt"] ?></srt>
<?php }
      if (isset($medias[$i]["parentPath"])) { ?>
      	    <parentPath><?= $medias[$i]["parentPath"] ?></parentPath>
<?php } ?>
	</media>
<?php } ?>
    </medias>
</browse>
